﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using JN.Utility;

namespace Power.Config
{
    public partial class SetDataBase : DevExpress.XtraEditors.XtraForm
    {
        public SetDataBase()
        {
            InitializeComponent();
        }

        private void SetDataBase_Load(object sender, EventArgs e)
        {
            //load config
            LoadConfig();
        }
        private void LoadConfig()
        {
            try
            {
                string file = Application.StartupPath + "\\Config\\SystemConfig.xml";
                if (!System.IO.File.Exists(file)) return;
                SystemConfig.Instance.LoadConfig(file);
                txtIp.Text = SystemConfig.Instance.DbIp;
                txtPwd.Text = Common.DEncrypt.DEncrypt.Decrypt(SystemConfig.Instance.DbPwd);
                txtSchema.Text = SystemConfig.Instance.Schema;
                txtUid.Text = SystemConfig.Instance.DbUid;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            //test connn
            if (!IsVaild())
            {
                return;
            }
            if (CheckConn())
            {
                XtraMessageBox.Show("连接数据库正常!!", "提示");
            }
            else
            {
                XtraMessageBox.Show("连接数据库失败!!", "提示");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //save 
            if (!IsVaild())
            {
                return;
            } 
            if (SaveConfig())
            {
                XtraMessageBox.Show("保存成功!!", "提示");
                this.Close();
            }
            else
            {
                XtraMessageBox.Show("保存失败!!", "提示");
            }
        }
        /// <summary>
        /// 输入检查
        /// </summary>
        /// <returns></returns>
        private bool IsVaild()
        {

            try
            {
                if (string.IsNullOrEmpty(txtIp.Text))
                {
                    XtraMessageBox.Show("请输入服务器!!", "提示");
                    txtIp.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtPwd.Text))
                {
                    XtraMessageBox.Show("请输入密码!!", "提示");
                    txtPwd.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtSchema.Text))
                {
                    XtraMessageBox.Show("请输入数据库!!", "提示");
                    txtSchema.Focus();
                    return false;
                }
                if (string.IsNullOrEmpty(txtUid.Text))
                {
                    XtraMessageBox.Show("请输入用户!!", "提示");
                    txtUid.Focus();
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                return false;
            }
        }
        /// <summary>
        /// 测试连接
        /// </summary>
        /// <returns></returns>
        private bool CheckConn()
        {
            try
            {
                bool ret = false;
                StringBuilder connString = new StringBuilder();
                connString.Append("server=" + txtIp.Text);
                connString.Append(";database=" + txtSchema.Text);
                connString.Append(";uid=" + txtUid.Text);
                connString.Append(";pwd=" + txtPwd.Text);

                
                DBUtility.DbHelperSQL.connectionString = connString.ToString();
                ret = DBUtility.DbHelperSQL.TabExists("sys_user");
                return ret;
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                return false;
            }
        }
        private bool SaveConfig()
        {
            try
            {                      
                string file = Application.StartupPath + "\\Config\\";
                if (!System.IO.Directory.Exists(file))
                {
                    System.IO.Directory.CreateDirectory(file);
                }
                SystemConfig config = SystemConfig.Instance;
                config.DbIp = txtIp.Text;
                config.DbPwd = Common.DEncrypt.DEncrypt.Encrypt(txtPwd.Text);
                config.DbUid = txtUid.Text;
                config.Schema = txtSchema.Text;

                System.Xml.Serialization.XmlSerializer serial = new System.Xml.Serialization.XmlSerializer(typeof(SystemConfig));
                System.IO.TextWriter writer = new System.IO.StreamWriter(file+"SystemConfig.xml");
                serial.Serialize(writer, config);
                writer.Close();
                writer.Dispose();       

                return true;
                     
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                return false;
            }
        }
    }
}